package com.rytong.tools.ui;

import com.rytong.tools.R;
import com.rytong.tools.httpconnect.WaitDialog.Task;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * WaitProgressDialog.java
 * 
 * @author cheng.l 2013-6-24 下午04:36:38
 */
public class WaitProgressDialog {
	private Activity activity_;
	private Task task_;
	public Dialog dialog_;
	private TextView message_;
	
	private String msg = null;
	public WaitProgressDialog(Activity activity, String msg) {
		activity_ = activity;
		this.msg = msg;
		init();
	}
	
	public WaitProgressDialog(Activity activity, Task task) {
		activity_ = activity;
		task_ = task;
		init();
	}
	
	public WaitProgressDialog(Activity activity) {
		activity_ = activity;
		init();
	}
	
	private void init() {
		// TODO Auto-generated method stub
		RelativeLayout line = (RelativeLayout) LayoutInflater.from(activity_).inflate(R.layout.layout_wait_progress, null);
		message_ = (TextView)line.findViewById(R.id.tv);
		if(null != msg){
			message_.setText(msg);			
		}else{
			message_.setText(R.string.loading_message);		
		}
		
		dialog_ = new Dialog(activity_, R.style.dialog);// 在values下创建一个styles.xml文件，代码见后
		dialog_.setContentView(line);
		Window mWindow = dialog_.getWindow();
		mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.dimAmount = 0f; // 弹出对话框后去除变暗阴影
		
		dialog_.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface dialog) {
				if(null != task_){
					task_.setStop(true);
				}
				Utils.SYN_REQUEST_NUMBER_ = 0;
				Utils.SYN_SUBMIT_REQUEST_NUMBER_ = 0;
			}
		});
	}

	public void show(final String msg) {
		if(!activity_.isFinishing()){
			activity_.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if(null != dialog_ && !dialog_.isShowing()){
						dialog_.show();
					}
				}
			});			
		}
	}
	
	public void show(final String msg, final boolean isGone) {
		if(!activity_.isFinishing()){
			activity_.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					if(null != dialog_ && !dialog_.isShowing()){
						dialog_.show();
						if (isGone) {
							dialog_.setOnKeyListener(new DialogOnKeyListener());
						}
					}
				}
			});			
		}
	}
	
	class DialogOnKeyListener implements OnKeyListener {

		@Override
		public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				return true;
			} else {
				return false;
			}
		}
		
	}
	
	public boolean isShowing(){
		return dialog_.isShowing();
	}
	
	public void dismiss() {
		if(!activity_.isFinishing()){
			activity_.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if(null != dialog_ && dialog_.isShowing()){
						dialog_.dismiss();
					}
				}
			});
		}
	}
	
	public void setMessage(final String msg){
		activity_.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				message_.setText(R.string.loading_message);
			}
		});
	}
}
